<?php

namespace App\Models;

use App\Models\airline\base\BanModel;
use App\Models\airline\base\PortModel;
use App\Models\base\WarehouseModel;
use App\Models\dispatch\UnloadModel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model as BaseModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;


class FlightModel extends BaseModel
{
    use HasFactory, SoftDeletes;

    protected $table = 't_flight';

    protected $primaryKey = 'id';

    public $timestamps = true;

    const CREATED_AT = 'created_at';
    
    const UPDATED_AT = null;

    protected $guarded = [];

    protected $hidden = [
        'deleted_at',
    ];

    //状态 1配舱中 2配舱完成
    const STATUS_PROGRESS = 1;
    const STATUS_COMPLETED = 2;
    const STATUS_MAPS = [
        self::STATUS_PROGRESS => '配舱中',
        self::STATUS_COMPLETED => '配舱完成'
    ];

    //创建人
    public function create_admin(): BelongsTo {
        return $this->belongsTo(AdminModel::class, 'created_admin_id');
    }

    //板型
    public function bans(): HasMany {
        return $this->hasMany(FlightBanModel::class, 'flight_id');
    }

    //航班信息
    public function items(): HasMany {
        return $this->hasMany(FlightItemModel::class, 'flight_id');
    }

    //卸货点
    public function unload_warehouse(): BelongsTo {
        return $this->belongsTo(UnloadModel::class, 'unload_warehouse_id');
    }

    //提单
    public function take_orders(): HasMany {
        return $this->hasMany(TakeOrderModel::class, 'flight_id');
    }

    //入仓单
    public function orders(): HasMany {
        return $this->hasMany(OrderModel::class, 'flight_id');
    }

    //目的地口岸
    public function destination_port(): BelongsTo {
        return $this->BelongsTo(PortModel::class, 'destination_port_id');
    }

    //始发地口岸
    public function start_port(): BelongsTo {
        return $this->BelongsTo(PortModel::class, 'start_port_id');
    }

    //目的地国家
    public function arrive_country(): BelongsTo {
        return $this->BelongsTo(CountryModel::class, 'arrive_port_country_id');
    }

    //始发地国家
    public function start_country(): BelongsTo {
        return $this->BelongsTo(CountryModel::class, 'start_port_country_id');
    }

}
